package com.yjx.gulimall.product.service.impl;

import com.yjx.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.yjx.gulimall.product.entity.AttrEntity;
import com.yjx.gulimall.product.service.AttrAttrgroupRelationService;
import com.yjx.gulimall.common.vo.product.AttrGroupVO;
import com.yjx.gulimall.common.vo.product.AttrVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.product.dao.AttrGroupDao;
import com.yjx.gulimall.product.entity.AttrGroupEntity;
import com.yjx.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrServiceImpl attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        IPage<AttrGroupEntity> iPage = new Query<AttrGroupEntity>().getPage(params);
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper = queryWrapper.and(obj -> {
                obj.like("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (categoryId != 0) {
            queryWrapper = queryWrapper.eq("catelog_id", categoryId);
        }
        return new PageUtils(this.page(iPage,queryWrapper));
    }

    @Override
    public List<AttrGroupVO> getAttrGroupAttrs(Long catelogId) {

        //获取当前分类下的所有的属性分组
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //AttrGroupEntity 转成 AttrGroupVO
        List<AttrGroupVO> attGroupVOList = attrGroupEntities.stream().map(obj -> {
            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(obj, attrGroupVO);
            return attrGroupVO;
        }).collect(Collectors.toList());


        attGroupVOList.forEach(attrGroupVO -> {
            //查找当前属性分组对应的属性
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = attrAttrgroupRelationService.list(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupVO.getAttrGroupId()));

            //获取当前分组下所有的Attr ID
            List<Long> attrId = attrgroupRelationEntityList.stream()
                    .map(attrAttrgroupRelationEntity -> {
                        return attrAttrgroupRelationEntity.getAttrId();
                    })
                    .collect(Collectors.toList());

            //查询当前分组下的所有属性
            if (!attrId.isEmpty()){
                List<AttrEntity> attrEntityList = attrService.listByIds(attrId);
                List<AttrVO> attrVOList = attrEntityList.stream().map(obj -> {
                    AttrVO attrVO = new AttrVO();
                    BeanUtils.copyProperties(obj, attrVO);
                    return attrVO;
                }).collect(Collectors.toList());
                attrGroupVO.setAttrs(attrVOList);
            }

        });
        return attGroupVOList;
    }

}